package com.czy.owner.ui.workorder;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.WorkOrderDetailsModel;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.StatusBarUtil;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.TimeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hedepu on 2017-09-04
 */

public class PaymentDetailsActivity extends BaseActivity {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_all_money)
    TextView tvOrderAllMoney;
    @BindView(R.id.relat_order_all_money)
    RelativeLayout relatOrderAllMoney;
    @BindView(R.id.tv_order_coupon_money)
    TextView tvOrderCouponMoney;
    @BindView(R.id.relat_order_coupon)
    RelativeLayout relatOrderCoupon;

    @BindView(R.id.tv_order_discount_money)
    TextView tvOrderDiscountmoney;
    @BindView(R.id.relat_order_discount)
    RelativeLayout relatOrderDiscount;

    @BindView(R.id.relat_order_balance)
    LinearLayout relatOrderBalance;
    @BindView(R.id.relat_order_depoist)
    LinearLayout relatOrderDepoist;
    @BindView(R.id.tv_depoist_time)
    TextView tvDepoistTime;
    @BindView(R.id.tv_balance_time)
    TextView tvBalanceTime;
    @BindView(R.id.tv_money_title)
    TextView tvMoneyTitle;
    @BindView(R.id.tv_balance_title)
    TextView tvBalanceTitle;
    @BindView(R.id.tv_start_balance)
    TextView tvStartBalance;
    @BindView(R.id.linear_start_balance)
    LinearLayout linearStartBalance;
    @BindView(R.id.linear_start_other_title)
    TextView linearStartOtherTitle;
    @BindView(R.id.linear_start_other_money)
    TextView linearStartOtherMoney;
    @BindView(R.id.linear_start_other)
    LinearLayout linearStartOther;
    @BindView(R.id.tv_balance_end_title)
    TextView tvBalanceEndTitle;
    @BindView(R.id.tv_balance_end_money)
    TextView tvBalanceEndMoney;
    @BindView(R.id.linear_balance_end)
    LinearLayout linearBalanceEnd;
    @BindView(R.id.tv_end_other_title)
    TextView tvEndOtherTitle;
    @BindView(R.id.linear_end_other_money)
    TextView linearEndOtherMoney;
    @BindView(R.id.linear_end_other)
    LinearLayout linearEndOther;
    @BindView(R.id.tv_end_coupon_money)
    TextView tvEndCouponMoney;
    @BindView(R.id.linear_end_other_coupon)
    LinearLayout linearEndOtherCoupon;

    @BindView(R.id.tv_cancle_time)
    TextView tvCancleTime;
    @BindView(R.id.tv_remarks)
    TextView tvRemarks;

    @BindView(R.id.linear_cancle_other)
    LinearLayout linearCancleOther;

    @BindView(R.id.tv_cost)
    TextView tvCost;
    @BindView(R.id.tv_cancle_type)
    TextView tvCancleType;
    @BindView(R.id.linear_cancle_type)
    LinearLayout linearCancleType;
    @BindView(R.id.tv_end_other_title_wx)
    TextView tvEndOtherTitleWx;
    @BindView(R.id.linear_end_other_money_wx)
    TextView linearEndOtherMoneyWx;
    @BindView(R.id.linear_end_other_wx)
    LinearLayout linearEndOtherWx;
    @BindView(R.id.tv_end_other_title_card)
    TextView tvEndOtherTitleCard;
    @BindView(R.id.linear_end_other_money_card)
    TextView linearEndOtherMoneyCard;
    @BindView(R.id.linear_end_other_card)
    LinearLayout linearEndOtherCard;
    @BindView(R.id.tv_end_other_title_cash)
    TextView tvEndOtherTitleCash;
    @BindView(R.id.linear_end_other_money_cash)
    TextView linearEndOtherMoneyCash;
    @BindView(R.id.linear_end_other_cash)
    LinearLayout linearEndOtherCash;

    @BindView(R.id.tv_end_other_title_person_hang)
    TextView tvEndOtherTitlePersonHang;
    @BindView(R.id.linear_end_other_money_person_hang)
    TextView linearEndOtherMoneyPersonHang;
    @BindView(R.id.linear_end_other_person_hang)
    LinearLayout linearEndOtherPersonHang;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_coupon_minimum)
    TextView tvCouponMinimum;
    @BindView(R.id.view_line_software)
    View viewLineSoftware;
    private String type = "1";//1为工单详情跳转。2为预约详情
    @BindView(R.id.tv_balance_content)
    TextView tvBalanceContent;

    @BindView(R.id.relat_balance_content)
    RelativeLayout relatbalancecontent;

    @BindView(R.id.tv_balance_all_money)
    TextView tvBalanceAllMoney;

    @BindView(R.id.tv_deposit_all_money)
    TextView tvDepositAllMoney;
    //订金挂账
    @BindView(R.id.tv_deposit_bill_money)
    TextView tvDepositBillMoney;
    //订金挂账
    @BindView(R.id.linear_deposit_bill)
    LinearLayout linearDepositBill;
    //订金挂账
    @BindView(R.id.tv_balance_bill_money)
    TextView tvBalanceBillMoney;
    //订金挂账
    @BindView(R.id.linear_balance_bill)
    LinearLayout linearBalanceBill;
    @BindView(R.id.view_cancle)
    View viewCancle;

    @BindView(R.id.view_balance_top)
    View viewBalanceTop;

    private WorkOrderDetailsModel workOrderDetailsModel;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_payment_details;
    }

    @Override
    protected void initInjector() {
        workOrderDetailsModel = (WorkOrderDetailsModel) getIntent().getSerializableExtra("orderDetails");
        type = getIntent().getStringExtra("type");
        tvOrderNum.setText(getIntent().getStringExtra("titleMoney").replace("¥", ""));
        tvMoneyTitle.setText(getIntent().getStringExtra("titleText") + "(元)");
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white));
    }

    @Override
    protected void initViews() {
        toolbarTitle.setText("支付详情");
        toolbarTitle.setTextColor(getResources().getColor(R.color.appThemeTitleColor));
        tvOrderAllMoney.setText("¥" + workOrderDetailsModel.getTotalPrice());
        toolbar.setBackgroundColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.icon_skin_previous);

        //待服务
        if (workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.WAITSERVICE)) {
//尾款内容隐藏
            relatOrderBalance.setVisibility(View.GONE);
            if (StringUtils.isEmpty(workOrderDetailsModel.getPayedDepositAmount()) || Double.parseDouble(workOrderDetailsModel.getPayedDepositAmount()) == 0) {
                relatOrderDepoist.setVisibility(View.GONE);
//                tvOrderNum.setText("¥0.00");
            } else {
                setServiceData();
            }
            //服务中
        } else if ((workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.ININSPECTION) || workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.INSERVICE)) && !StringUtils.isEmpty(workOrderDetailsModel.getStoreBookingNumber())) {
            //尾款内容隐藏
            relatOrderBalance.setVisibility(View.GONE);

            if (StringUtils.isEmpty(workOrderDetailsModel.getPayedDepositAmount()) || Double.parseDouble(workOrderDetailsModel.getPayedDepositAmount()) == 0) {
                relatOrderDepoist.setVisibility(View.GONE);
//                tvOrderNum.setText("¥0.00");
            } else {
                setServiceData();
            }

        }
        //结算尾款
        else if (workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.INSETTLEMENT)) {
//            if (StringUtils.isEmpty(workOrderDetailsModel.getStoreBookingNumber()) || Double.parseDouble(workOrderDetailsModel.getStoreBookingNumber()) != 0) {
            //尾款内容隐藏
            relatOrderBalance.setVisibility(View.GONE);

            if (StringUtils.isEmpty(workOrderDetailsModel.getPayedDepositAmount()) || Double.parseDouble(workOrderDetailsModel.getPayedDepositAmount()) == 0 || StringUtils.isEmpty(workOrderDetailsModel.getStoreBookingNumber())) {
                relatOrderDepoist.setVisibility(View.GONE);
//                tvOrderNum.setText("¥0.00");
            } else {
                setServiceData();
            }
        } else if (workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.COMPLETED)) {
            setServiceData();
            setBanData();
            // 已评价commented
        } else if (workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.COMMENTED)) {
            setServiceData();
            setBanData();

        } else if (workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.CANCELLED)) {
//            viewCancle.setVisibility(View.VISIBLE);
            relatOrderCoupon.setVisibility(View.GONE);
            relatOrderBalance.setVisibility(View.GONE);
            linearCancleOther.setVisibility(View.VISIBLE);
            linearCancleType.setVisibility(View.VISIBLE);
            viewLineSoftware.setVisibility(View.GONE);

            tvCancleTime.setText(TimeUtils.timeStamp2Date(workOrderDetailsModel.getDepositPayDetail().getRefundTime(), "yyyy-MM-dd HH:mm"));
//            tvMoneyTitle.setText("实付金额");

            setServiceData();
//            tvOrderNum.setText("¥0.00");
            //待付余款
        }
        if (StringUtils.isEmpty(workOrderDetailsModel.getStoreBookingNumber())) {
            relatOrderDepoist.setVisibility(View.GONE);
        }


        tvOrderAllMoney.setText("¥" + workOrderDetailsModel.getTotalPrice());
//        if ("2".equals(type)) {
//            tvMoneyTitle.setText("已付金额");
//            tvRemarks.setVisibility(View.GONE);
//        }

        //修改之前的各种实付，已付统一，，在此处统一修改
//        tvMoneyTitle.setText("已付金额");
//        tvRemarks.setVisibility(View.GONE);
    }

    //设置订金支付明细
    public void setServiceData() {

        //订金为0时候，除了是取消的，隐藏订金
        if (workOrderDetailsModel.getDepositPayDetail() == null || Double.parseDouble(StringUtils.getString(workOrderDetailsModel.getDepositPayDetail().getPayedDepositAmount(), "0")) == 0) {
            if (!workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.CANCELLED)) {
                relatOrderDepoist.setVisibility(View.GONE);
                return;
            }
        }
        if (workOrderDetailsModel.getOrderStatus().equals(OrderDetailsActivity3.CANCELLED)) {
            //退的订金
            tvDepositAllMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDepositPayDetail().getDepositRefund()));
        } else {
            //总的订金
            tvDepositAllMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDepositPayDetail().getPayedDepositAmount()));
        }

        //支付时间
        if (workOrderDetailsModel.getDepositPayDetail() != null) {
            tvDepoistTime.setText(TimeUtils.timeStamp2Date(workOrderDetailsModel.getDepositPayDetail().getPayTime(), "yyyy-MM-dd HH:mm"));
        }
        //余额支付
        if (StringUtils.isEmpty(workOrderDetailsModel.getDepositPayDetail().getBalancePay())) {
            tvStartBalance.setText("¥0.00");
            linearStartBalance.setVisibility(View.GONE);
        } else {
            tvStartBalance.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDepositPayDetail().getBalancePay()));
        }
        //挂账支付
        if (StringUtils.isEmpty(workOrderDetailsModel.getDepositPayDetail().getHang())) {

            linearDepositBill.setVisibility(View.GONE);
        } else {
            tvDepositBillMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDepositPayDetail().getHang()));
        }
        //
//        tvOrderNum.setText("¥" + DecimalUtil.sub(workOrderDetailsModel.getDepositPayDetail().getPayedDepositAmount(), workOrderDetailsModel.getDepositPayDetail().getBalancePay()));
        //修改为已付
//        tvOrderNum.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDepositPayDetail().getPayedDepositAmount()));
        if (StringUtils.isEmpty(workOrderDetailsModel.getDepositPayDetail().getAliPay())) {
//            linearStartBalance.setVisibility(View.VISIBLE);
        } else {
            linearStartOtherTitle.setText("支付宝(线上)：");
            linearStartOtherMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDepositPayDetail().getAliPay()));
        }
        if (StringUtils.isEmpty(workOrderDetailsModel.getDepositPayDetail().getWxPay())) {

        } else {
            linearStartOtherTitle.setText("微信(线上)：");
            linearStartOtherMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDepositPayDetail().getWxPay()));
        }

        if (StringUtils.isEmpty(linearStartOtherMoney.getText().toString())) {
            linearStartOther.setVisibility(View.GONE);
//            linearStartOtherTitle.setText("其他支付：");
//            linearStartOtherMoney.setText("¥0.00");
        }
//        if ("2".equals(type)) {
//            tvOrderNum.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDepositPayDetail().getPayedDepositAmount()));
//
//        }
    }

    //设置余款明细
    public void setBanData() {
//        TextView tvBalanceEndTitle;
//        TextView tvBalanceEndMoney;
//        LinearLayout linearBalanceEnd;
//        TextView tvEndOtherTitle;
//        TextView linearEndOtherMoney;
//        LinearLayout linearEndOtherTitle;
        if (workOrderDetailsModel.getBalancePayDetail() == null || Double.parseDouble(StringUtils.getString(workOrderDetailsModel.getBalancePayDetail().getActualAmount(), "0")) == 0) {
            relatOrderBalance.setVisibility(View.GONE);

            return;
        }
        if (workOrderDetailsModel.getBalancePayDetail() == null) {
            return;
        }
        if (workOrderDetailsModel.getBalancePayDetail() != null) {
            tvBalanceTime.setText(TimeUtils.timeStamp2Date(workOrderDetailsModel.getBalancePayDetail().getPayTime(), "yyyy-MM-dd HH:mm"));
        }
        tvBalanceAllMoney.setText("¥" + DecimalUtil.sub(StringUtils.getString(workOrderDetailsModel.getBalancePayDetail().getActualAmount(), "0"),
                DecimalUtil.add(StringUtils.getString(workOrderDetailsModel.getBalancePayDetail().getCouponsPay(), "0"), workOrderDetailsModel.getDiscountAmountMoney())));
        //优惠券
        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getCouponsPay())) {

        } else {
            relatOrderCoupon.setVisibility(View.VISIBLE);
            tvOrderCouponMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getCouponsPay()));
//            if (workOrderDetailsModel.getBalancePayDetail().getMinimum() > 0) {
            tvCouponMinimum.setText("(满" + workOrderDetailsModel.getBalancePayDetail().getMinimum() + "-" + workOrderDetailsModel.getBalancePayDetail().getCouponsPay() + ")");

        }
        //优惠金额
        if (StringUtils.isEmpty(workOrderDetailsModel.getDiscountAmountMoney()) || Double.parseDouble(workOrderDetailsModel.getDiscountAmountMoney()) == 0) {

        } else {
            relatOrderDiscount.setVisibility(View.VISIBLE);
            tvOrderDiscountmoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getDiscountAmountMoney()));
        }
        if (relatOrderCoupon.getVisibility() == View.GONE && relatOrderDiscount.getVisibility() == View.GONE) {
            viewLineSoftware.setVisibility(View.GONE);
        }
        //挂账支付
        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getHang())) {

            linearBalanceBill.setVisibility(View.GONE);
        } else {
            tvBalanceBillMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getHang()));
        }
        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getBalancePay())) {
            linearBalanceEnd.setVisibility(View.GONE);
            tvBalanceEndMoney.setText("¥0.00");

        } else {
            tvBalanceEndMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getBalancePay()));
            if (workOrderDetailsModel.getFriendMember() != null) {
                tvCost.setVisibility(View.VISIBLE);
                tvBalanceEndTitle.setText("余额支付（借）：");
                tvCost.setText("被借人：" + workOrderDetailsModel.getFriendMember().getNoteName() + " " + workOrderDetailsModel.getFriendMember().getPhone());
            }

        }

//        String balanceMoney;
//        if (StringUtils.isEmpty(workOrderDetailsModel.getStoreBookingNumber())) {
//
//            balanceMoney = StringUtils.getString(workOrderDetailsModel.getBalancePayDetail().getBalancePay());
//        } else {
//            balanceMoney = DecimalUtil.add(StringUtils.getString(workOrderDetailsModel.getDepositPayDetail().getBalancePay()),
//                    StringUtils.getString(workOrderDetailsModel.getBalancePayDetail().getBalancePay()));
//        }
//        balanceMoney = DecimalUtil.add(balanceMoney, StringUtils.getString(workOrderDetailsModel.getDepositPayDetail().getCouponsPay()));

        if (StringUtils.isEmpty(workOrderDetailsModel.getStoreBookingNumber())) {
            relatbalancecontent.setVisibility(View.GONE);
            viewBalanceTop.setBackgroundColor(this.getResources().getColor(R.color.white));

//            viewCancle.setVisibility(View.VISIBLE);
//            tvOrderNum.setText("¥" + DecimalUtil.sub(workOrderDetailsModel.getBalancePayDetail().getActualAmount(), DecimalUtil.add(couponsMoney, workOrderDetailsModel.getDiscountAmountMoney())));
        } else {
            //  tvOrderNum.setText("¥" + DecimalUtil.sub(DecimalUtil.sub(workOrderDetailsModel.getTotalPrice(), balanceMoney), DecimalUtil.add(couponsMoney, workOrderDetailsModel.getDiscountAmountMoney())));

        }
//        tvOrderNum.setText("¥" + DecimalUtil.sub(workOrderDetailsModel.getTotalPrice(), DecimalUtil.add(couponsMoney, workOrderDetailsModel.getDiscountAmountMoney())));
        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getAliPay())) {
//            linearStartBalance.setVisibility(View.VISIBLE);
        } else {
            if (workOrderDetailsModel.getBalancePayDetail().isOfflinePay()) {
                tvEndOtherTitle.setText("支付宝(线下)：");
            } else {
                tvEndOtherTitle.setText("支付宝(线上)：");
            }
            linearEndOtherMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getAliPay()));
            linearEndOther.setVisibility(View.VISIBLE);
        }
        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getWxPay())) {

        } else {
            if (workOrderDetailsModel.getBalancePayDetail().isOfflinePay()) {
                tvEndOtherTitleWx.setText("微信(线下)：");
            } else {
                tvEndOtherTitleWx.setText("微信(线上)：");

            }
            linearEndOtherMoneyWx.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getWxPay()));
            linearEndOtherWx.setVisibility(View.VISIBLE);
        }
        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getCreditCard())) {

        } else {
            tvEndOtherTitleCard.setText("银行卡：");
            linearEndOtherMoneyCard.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getCreditCard()) + "");
            linearEndOtherCard.setVisibility(View.VISIBLE);
        }
        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getCash())) {

        } else {
            tvEndOtherTitleCash.setText("现金：");
            linearEndOtherMoneyCash.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getCash()));
            linearEndOtherCash.setVisibility(View.VISIBLE);
        }
        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getPersonHang())) {

        } else {
            linearEndOtherMoneyPersonHang.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getPersonHang()) + "");
            linearEndOtherPersonHang.setVisibility(View.VISIBLE);
        }
//        if (StringUtils.isEmpty(workOrderDetailsModel.getBalancePayDetail().getMobile())) {
//
//        } else {
//            tvEndOtherTitle.setText("线下支付：");
//            linearEndOtherMoney.setText("¥" + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getMobile()) + "（移动支付支付）");
//        }
        //不包含其他支付隐藏
//        if (StringUtils.isEmpty(linearEndOtherMoney.getText().toString())) {
//            linearEndOther.setVisibility(View.GONE);

//            tvEndOtherTitle.setText("其他支付：");
//            linearEndOtherMoney.setText("¥0.00");
//            + DecimalUtil.formatNum(workOrderDetailsModel.getBalancePayDetail().getMobile()) + "（移动支付支付）"
//        }

    }

    @Override
    protected void updateViews() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
